import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# 读取本地CSV文件
file_path = "D:\\python vs\\qie.csv"
df = pd.read_csv(file_path)

# 打印数据集的前5行
print(df.head())

# 使用matplotlib绘制企鹅种类的条形图
df['Species'].value_counts().plot(kind='bar')
plt.xlabel('Species')
plt.ylabel('Count')
plt.title('Distribution of Penguin Species')
plt.show()

# 使用seaborn绘制箱型图来观察不同种类企鹅的FlipperLength、CulmenLength和CulmenDepth的分布
sns.boxplot(data=df, x='Species', y='FlipperLength')
plt.show()

sns.boxplot(data=df, x='Species', y='CulmenLength')
plt.show()

sns.boxplot(data=df, x='Species', y='CulmenDepth')
plt.show()

# 显示包含缺失值的行
print(df[df.isnull().any(axis=1)])

# 删除包含缺失值的行
df = df.dropna()

# 准备训练数据
X = df[['CulmenLength', 'CulmenDepth', 'FlipperLength']]
y = df['Species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 训练逻辑回归模型
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)
model.fit(X_train, y_train)

# 预测测试集标签
y_pred = model.predict(X_test)

# 计算模型准确率
accuracy = accuracy_score(y_test, y_pred)
print(f'Model Accuracy: {accuracy:.2f}')